const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const expressjwt = require('express-jwt').expressjwt

// 连接数据库
require('./db/index')

const userRouter = require('./router/user')
const bookRouter = require('./router/book')

const app = express()

app.use(cors())
// 处理 post 参数
app.use(express.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.use(
  expressjwt({
    secret: 'nodejs',
    algorithms: ['HS256'],
  }).unless({
    path: [
      //接口白名单, 这里的接口时免除token验证的
      '/user/login',
      '/user/register',
    ],
  })
)

// 登录
app.use('/user', userRouter)

// 图书信息
app.use('/book', bookRouter)

app.listen(3000, () => {
  console.log('服务器启动成功')
})
